<?php
/**
 * 登录管理
 * User: raisound
 * Date: 2021/10/13
 */
namespace app\api\controller;
use app\BaseController;
use think\facade\Db;
class Login extends BaseController
{
    public function initialize()
    {
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Headers: X-Token,Content-Type, Content-Length, Authorization, Accept, X-Requested-With, Current-Page');
        header('Access-Control-Allow-Methods: POST,GET,OPTIONS,DELETE,PUT');
        parent::initialize();
    }

    /**
     * 用户登录验证
     * @return \josn
     */
    public function signIn()
    {
        try{
            // 数据接收
            $user_number = input("param.number");
            $user_pass = input("param.password");
            $data = [
                'number' =>$user_number,
                'password' =>$user_pass,
            ];
            // 验证字段
            $validate = (new \app\api\validate\UserInfo())->scene('sign_in');
            if(!$validate->check($data)){
                return returnResult(config("config.code.error"),$validate->getError());
            }
            // 业务处理
            $result = \app\api\business\LoginBus::login($data);
            if($result){
                return returnResult(config("config.code.success"),config("config.describe.login_successful"),$result);
            }else{
                return returnResult(config("config.code.error"),config("config.describe.login_failed"));
            }
        }catch (\Exception $e){
            exception($e,'Login.signIn');
        }
    }

//    /**
//     * 通过token获取用户信息
//     * @return \josn
//     */
//    public function tokenInfo()
//    {
//        // 业务处理
//        try{
//            // 参数接收
//            $token = input("token");
//            $bus = new UserInfoBus();
//            $result = $bus->getUserToken($token);
//            $q = Db::table('menu')->select();
//            $list = [];
//            foreach ($q as $index =>$value) {
//                if($value['levels']==0){
//                    $q=[
//                        'path'=>$value['url'],
//                        'component'=>"Layout",
//                        'alwaysShow'=>true,
//                        'redirect'=>'',
//                        'children'=>[
//                            'path'=>$value['url'],
//                            'component'=>"import('@/views/label/index')",
//                            'alwaysShow'=>true,
//                            'name'=>$value['url'],
//                            'meta'=>"{ title: ".$value['title'].", icon: 'size', affix: true }"
//                        ]
//                    ];
//                    $list[$value['id']]=$q;
//                }else{
//                    $s = [
//                        'path'=>$value['url'],
//                        'component'=>"import('@/views/label/index')",
//                        'alwaysShow'=>true,
//                        'name'=>$value['url'],
//                        'meta'=>"{ title: ".$value['title'].", icon: 'size', affix: true }"
//                    ];
//                    $list[$value['id']]['children']=$s;
//                }
//            }
//            var_dump($list);
//            $data = array('data'=>$result,'roles'=>$list);
//            if($result){
//                return returnResult(config("config.code.success"),config("config.describe.success"),$data);
//            }else{
//                return returnResult(config("config.code.success"),config("config.describe.null_data"),[]);
//            }
//        }catch (\Exception $e){
//            logWrite('tokenInfo', $e->getMessage());
//            return returnResult(config("config.code.error"),config("config.describe.error"));
//        }
//
//    }
}
